package com.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.mapper.UserMapper;
import com.user.pojo.Position;
import com.user.pojo.User;
import com.user.pojo.data;
import com.user.reuslt.Result;
import com.user.service.PositionService;
import com.user.mapper.PositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【position(职位表)】的数据库操作Service实现
 * @createDate 2024-09-20 10:20:16
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService
{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;


    @Override
    public Result queryPositon(data data)
    {
        LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<Position>().like(Position::getPositionName, data.getPositionName());
        List<Position> data1 = positionMapper.selectList(positionWrapper);
        System.out.println(data1);

        LambdaQueryWrapper<User> UserWrapper = new LambdaQueryWrapper<User>().eq(User::getPositionId, data1.get(0).getId());
        List<User> data2 = userMapper.selectList(UserWrapper);

        System.out.println(data2);

        List<Object> Data0 = new ArrayList<>();
        Data0.addAll(data1);
        Data0.addAll(data2);

        return Result.data(Data0);
    }

    @Override
    public Result Insertposition(Position position)
    {
        positionMapper.insert(position);
        return Result.data("OK");
    }

    @Override
    public Result Updateposition(data data)
    {
        LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<Position>().eq(Position::getPositionName, data.getPositionName());
        Position position = positionMapper.selectOne(positionWrapper);
        position.setPositionName(data.getPositionName());
        position.setPermissions(data.getPermissions());
        positionMapper.updateById(position);
        return Result.data("OK");
    }

    public void upDeleteUser(Integer id){
        positionMapper.upDeleteUser(id);
    };
    public  void deleteP(Integer id){
        positionMapper.deleteP(id);
    }
    @Override
    public Result DeleteService(data data)
    {

        LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<Position>().eq(Position::getPositionName, data.getPositionName());
        List<Position> data1 = positionMapper.selectList(positionWrapper);
 /*
        LambdaUpdateWrapper<User> userWrapper = new LambdaUpdateWrapper<User>().eq(User::getPositionId, data1.get(0).getId());
        List<User> user = userMapper.selectList(userWrapper);
        for (User u : user)
        {
            u.setPositionId(null);
            userMapper.updateById(u);
        }
        positionMapper.deleteBatchIds(data1);
*/
        upDeleteUser(data1.get(0).getId());
        deleteP(data1.get(0).getId());

        return Result.data("OK");
    }
}




